package cn.blue.base.util;

import cn.blue.base.jwt.*;
import cn.blue.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

@Slf4j
public class LoginContext {


    public static Logininfo getLogininfo(HttpServletRequest request) {
        // 获取token
        try {
            String token = request.getHeader("token");
            PublicKey publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
            Payload<UserInfo> userInfoPayload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
            UserInfo userInfo = userInfoPayload.getUserInfo();
            // 差不到用户信息就是未登录
            if (userInfo == null) {
                log.info("用户未登录");
                return null;
            }
            // 查到了就返回用户信息
            return userInfo.getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
